package cn.glutnn.auth.application.controller;

import cn.glutnn.auth.application.convert.AuthPermissionConverter;
import cn.glutnn.auth.application.dto.AuthPermissionDTO;
import cn.glutnn.auth.application.entity.Result;
import cn.glutnn.auth.domain.entity.AuthPermissionBO;
import cn.glutnn.auth.domain.service.AuthPermissionDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:   msh
 * Date:     2025/1/14 下午1:44
 * Description:
 */

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController add authPermissionDTO:{}", authPermissionDTO);
            }
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(), "权限标识不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(), "权限类型不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getStatus(), "权限状态不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "父级id不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getMenuUrl(), "菜单url不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController add error:{}", e.getMessage(), e);
            return Result.fail("添加失败");
        }
    }

    // 更新
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController update authPermissionDTO:{}", authPermissionDTO);
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController update error:{}", e.getMessage(), e);
            return Result.fail("更新失败");
        }
    }

    // 删除权限
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController delete authPermissionDTO:{}", authPermissionDTO);
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController delete error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    // 查询用户权限
    @GetMapping("/getPermission")
    public Result<List<String>> getPermission(String userName) {
        try {
            log.info("AuthPermissionController getPermission userName:{}", userName);
            Preconditions.checkNotNull(userName, "用户名不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("AuthPermissionController getPermission error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }
}
